package com.ruimei.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruimei.entitys.Notice;
import com.ruimei.entitys.Student;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.IParentService;
import com.ruimei.service.NoticeService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@Api(tags={"通知"})
public class NoticeController
{
  @Resource
  NoticeService noticeService;
  @Resource
  IParentService parentService;
  @Resource
  private  RedisTemplate<String,String> redisTemplate;

  @ApiOperation(value="W获取学校通知", notes="传入id获得详情，不传获取所有通知")
  @PostMapping({"/notice/school"})
  @ResponseBody
  public Map<String, Object> getSchoolNotice(String noticeId,HttpServletRequest request)
  {
    Map<String, Object> map = this.noticeService.getSchoolNotice(noticeId);
    if(redisTemplate.hasKey("school_notice")&&redisTemplate.opsForHash().get("school_notice",TokenUtils.getTeacherId(request)).equals("0")){
      redisTemplate.opsForHash().increment("school_notice",TokenUtils.getTeacherId(request),1);
    }
    return map;
  }

  @ApiOperation(value="W获取班级通知通过教师ID", notes="传入id获得详情，不传获取所有通知")
  @PostMapping({"/notice/class"})
  @ResponseBody
  public Map<String, Object> getClassNotice(String noticeId, String callerId,String classId, HttpServletRequest request)
  {
    Teacher teacher ;
    Map<String, Object> map = null;
    if(redisTemplate.hasKey("class_notice"+classId)&&redisTemplate.opsForHash().get("class_notice"+classId,callerId).equals("0")){
      redisTemplate.opsForHash().increment("class_notice"+classId,callerId,1);
    }
    map = this.noticeService.getClassNotice(null, noticeId, classId);

    return map;
  }

  @ApiOperation(value="W获取缴费通知通过教师ID", notes="传入id获得详情，不传获取所有通知")
  @PostMapping({"/notice/pay"})
  @ResponseBody
  public Map<String, Object> getPayNotice( String noticeId, String classId,String callerId)
  {

    if(redisTemplate.hasKey("pay_notice"+classId)&&redisTemplate.opsForHash().get("pay_notice"+classId,callerId).equals("0")){
      redisTemplate.opsForHash().increment("pay_notice"+classId,callerId,1);
    }
    return this.noticeService.getPayNotice(null,noticeId,classId);
  }

  @ApiOperation(value="获取学校公告列表", notes="不需要传参，获取所有学校通知")
  @PostMapping({"/notice/pzh"})
  @ResponseBody
  public Map<String, Object> getSchoolList(String noticeId)
  {
    return this.noticeService.getSchoolList(noticeId);
  }


  @ResponseBody
  @RequestMapping(value = "/querystatus",method = RequestMethod.POST,produces = "application/json")
  public JSONObject queryStatus(String id,String classList,String identity,HttpServletRequest request) {
    JSONObject object = new JSONObject();
    Map<String,Object> objectMap = new HashMap<>(10);
    boolean classStatus = false;
    boolean payStatus = false;
    boolean classExists = false;
    boolean payExists = false;
    if (StringUtils.isNotBlank(classList)) {
      //如果不包含则返回本身
      String[] strings = classList.split(",");
      classTo:
      for (int i = 0; i < strings.length; i++) {
        if (redisTemplate.hasKey("class_notice" + strings[i])) {
          String class_notice = redisTemplate.opsForHash().get("class_notice" + strings[i], id).toString();
          classExists=true;
          if (class_notice.equals("1") ) {
            classStatus = true;
            break classTo;
          }
          else{
            classStatus = false;
          }
        }
        }
      payTo:
        for (int j = 0; j < strings.length; j++) {
          if (redisTemplate.hasKey("pay_notice" + strings[j])) {
            String class_notice = redisTemplate.opsForHash().get("pay_notice" + strings[j], id).toString();
            payExists=true;
            if (class_notice.equals("1") ) {
              payStatus = true;
              break payTo;
            }
            else{
              payStatus = false;
            }

          }

        }

    }


    object.put("msg", "查询完成");
    object.put("status", true);

    //缴费通知判断
    if(payExists){
      if (payStatus) {
        objectMap.put("pay_notice", "1");
      } else {
        objectMap.put("pay_notice", "0");
      }
    }

    //班级通知判断
  if(classExists){
    if (classStatus) {
      objectMap.put("class_notice", "1");
    } else {
      objectMap.put("class_notice", "0");
    }
  }
  if(identity.equals("parent")){
    JSONObject objectt = new LoginController().getChilds(request,redisTemplate,parentService);
    JSONObject object1 = (JSONObject) objectt.get("data");
    List<Student> students = (List<Student>) object1.get("data");
    for(Student s : students){
    if(s.getGradeName().indexOf("初") != -1&&!objectMap.containsKey("primary_notice")){
      objectMap.put("primary_notice",redisTemplate.opsForHash().get("primary_notice",id));
    }
      if(s.getGradeName().indexOf("小") != -1&&!objectMap.containsKey("junior_notice")){
        objectMap.put("junior_notice",redisTemplate.opsForHash().get("junior_notice",id));
      }
    }
  }

    object.put("data",objectMap);
    return object;

  }


  @ApiOperation(value="发送缴费/学校通知")
  @PostMapping(value = "/sendNotice")
  @ResponseBody
  public JSONObject sendNotice(Notice notice,HttpServletRequest request){
    Teacher teacher = JSON.parseObject(redisTemplate.opsForValue().get(TokenUtils.getTeacherId(request)),Teacher.class);
    notice.setTeacherName(teacher.getFullName());
    notice.setCreateUserId(teacher.getId());
    notice.setNoticeId(String.valueOf(System.currentTimeMillis()));
    return noticeService.sendNotice(notice,redisTemplate);
  }



  @ApiOperation(value="查看学校通知")
  @PostMapping(value = "/querySchoolNotice")
  @ResponseBody
  public JSONObject querySchoolNotice(String id,String type,String identity,String callerId,HttpServletRequest request){
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    boolean isLook = false;
    List<Notice>  notice = noticeService.querySchoolNotice(id,type);
    if(StringUtils.isBlank(id)){
      if(identity.equals("parent")){
        JSONObject objectt = new LoginController().getChilds(request,redisTemplate,parentService);
        JSONObject object1 = (JSONObject) objectt.get("data");
        List<Student> students = (List<Student>) object1.get("data");
        for(Student s : students){
          if(type.equals("PRIMARY")){
            if(s.getGradeName().indexOf("小") != -1){
              isLook = true;
              break;
            }

          }
          if(type.equals("JUNIOR")){
            if(s.getGradeName().indexOf("初") != -1){
              isLook = true;
              break;
            }
          }
        }
      }
      if(!isLook&&identity.equals("parent")){
        data.put("status",false);
        data.put("msg","您绑定的孩子不符合查看该类通知的权限");
        data.put("isLook",isLook);
        object.put("status",true);
        object.put("data",data);
        return object;
      }

      if(type.equals("JUNIOR")){
        if(redisTemplate.opsForHash().hasKey("junior_notice",callerId)) {
        if(redisTemplate.hasKey("junior_notice")&& redisTemplate.opsForHash().get("junior_notice",callerId).equals("0")){
            redisTemplate.opsForHash().increment("junior_notice", callerId, 1);
          }
        }
      }else{
        if(redisTemplate.opsForHash().hasKey("primary_notice",callerId)) {
        if(redisTemplate.hasKey("primary_notice") && redisTemplate.opsForHash().get("primary_notice",callerId).equals("0")){
            redisTemplate.opsForHash().increment("primary_notice", callerId, 1);
          }
        }
      }
    }




    data.put("status",true);
    data.put("data",notice);
    object.put("status",true);
    object.put("data",data);
    return object;

  }
}



/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\controller\NoticeController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */